#include "MainLoop.h"
#include "DxApp.h"
#include <iostream>
#include <math.h>
using namespace std;

#include "../Primitives/Line.h"
#include "../Primitives/Traingle.h"
#include "../Primitives/Quad.h"
#include "../Primitives/Cube.h"
#include "../Primitives/Sphere.h"
#include "../Math/Vector.h"
#include "../Math/Matrix.h"
#include "../Texture/Texture.h"
#include "../Light/LightMgr.h"
#include "../Light/PointLight.h"

PointLight* light = NULL;
Texture* txt = NULL;
Sphere* sp = NULL;
MainLoop::MainLoop(){
	light = new PointLight(Vec3(0,0,-500));
	LightMgr::getInstance()->addLight(light);
	sp = new Sphere(Point(200,200,200),200);
	txt = new Texture("pet.png");
	sp->setTexture(txt);
}

int angle = 0;
void MainLoop::loop( DxApp* dx )
{
	dx->setCullFrontFace(IPixelDrawer::CCW);
	Vec3* pos = &light->pos;
	pos->y = 200;
	pos->x = 400 * sin((float)angle/180 * 3.1415926) + 200;
	pos->z = 400 * cos((float)angle/180 * 3.1415926) + 200;
	//cout << pos->x << "=" << pos->z << ":a:"<<angle<<endl;

	dx->clearBuffer();

	unsigned char c = 255 * 1.3;
	COLOR color = RGBA(255, 0, 255,0);
	//Quad t(Point(200,200),200,200);
	Cube t(Point(200,200,200),200,200,200);
	t.setTexture(txt);
	Mat4 mx,my,mz,mto,mback,ms;
	//float sina = 3 * sin(angle * 3.1415926 / 180.0);
	//ms.scale(sina,sina,sina);
	mx.rotX(angle);
	my.rotY(angle);
	mz.rotZ(angle);
	angle += 10;
	angle = angle%360;
	//cout << "angle " << angle <<endl;
	mto.translate(-200,-200,-200);
	mback.translate(200,200,200);
	t.mulMat(mto);
	//t.mulMat(ms);
	t.mulMat(mx);
	t.mulMat(my);
	t.mulMat(mz);
	t.mulMat(mback);
	t.draw(dx);
	if(false)
	{
		sp->setShowBorder(true);
		sp->mulMat(mto);
		sp->mulMat(mx);
		sp->mulMat(my);
		sp->mulMat(mz);
		sp->mulMat(mback);
		sp->draw(dx);
	}
	//txt->draw(dx);

	dx->flushBuffer();
	Sleep(50);
}

void MainLoop::onKeyDown(WPARAM wParam)
{
	switch (wParam)
	{
	case VK_LEFT:
		//light->pos.z += 10;
		//cout << "Z+:" <<light->pos.z << endl;
		angle -= 1;
		cout << "angle:" << angle << endl;
		break;
	case VK_RIGHT:
		//light->pos.z -= 10;
		//cout << "Z-:" <<light->pos.z << endl;
		angle += 1;
		cout << "angle:" << angle << endl;
		break;
	default:
		break;
	}
	printf("onKeyDown %d\n",wParam);
}

void MainLoop::onMouseDown( int x,int y )
{
	printf("MouseDown %d,%d\n",x,y);
}

void MainLoop::onMouseMove( int x,int y )
{
	//printf("MouseMove %d,%d\n",x,y);
}


